<?php
class Common_Model_Currency
{
 
	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Common_Model_DbTable_Currency();
	}
	
	/**
	*add DB Adapter  
	*/	
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	/**
	*add One Row  
	*@param array $row 
	*@return boolean
	*/
	public function add($row){
		return $this->_table->insert($row);
	}
	
	
	public function getByCode($code){
		$select = $this->_table->select()->where('currency_code = ?', $code);
		$result = $this->_table->fetchRow($select);
		if($result instanceof Zend_Db_Table_Row){
			return $result->toArray();
		}
		return false;
	}
	 
	public function get($id){
		$where = $this->_table->getAdapter()->quoteInto('currency_id= ? ',$id);
		return $this->_table->fetchRow($where);
	}

	public function getByValue($value,$field='currency_id'){
		$select = $this->_table->getAdapter()->select();
		$table  = $this->_table->info('name');
		$select->from($table,'*');
		$select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}

	public function create($row){
	  return $this->_table->insert($row);
	} 

	public function update($row,$id){
	  $where = $this->_table->getAdapter()->quoteInto("currency_id= ?", $id);
	  return $this->_table->update($row,$where);
	}

	public function delete($id){
	  $where = $this->_table->getAdapter()->quoteInto("currency_id= ?",$id);
	  return $this->_table->delete($where);
	}

	public function getByCondition($condition,$type='*',$pageSize=0,$page=1,$orderby=array('currency_id')){
		$select = $this->_table->getAdapter()->select();
		$table = $this->_table->info('name');
		$select->from($table,$type);
		$select->where("1 =?",1);
		if(isset($condition['currency_name'])&&$condition['currency_name']!=""){
			$select->where(" currency_name like ?","%".$condition["currency_name"]."%");
		}
		if(isset($condition['currency_cn_name'])&&$condition['currency_cn_name']!=""){
			$select->where(" currency_cn_name like ?","%".$condition["currency_cn_name"]."%");
		}
		if(isset($condition['currency_code'])&&$condition['currency_code']!=""){
			$select->where(" currency_code like ?","%".$condition["currency_code"]."%");
		}
		if(isset($condition['currency_symbol_left'])&&$condition['currency_symbol_left']!=""){
			$select->where(" currency_symbol_left like ?","%".$condition["currency_symbol_left"]."%");
		}
		if('count(*)'==$type){
			return $this->_table->getAdapter()->fetchOne($select);
		}else{
			$select->order($orderby);
			if($pageSize>0 and $page>0)
			{
				$start =($page-1)*$pageSize;
				$select->limit($pageSize,$start);
			}
			$sql=$select->__toString();
			return $this->_table->getAdapter()->fetchAll($sql);
		}
	}
	
	public function getAll(){
		return $this->_table->fetchAll()->toArray();
	}
	 
}